package com.chaodev.base.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;


/**
 * 返回的对象(统一返回)
 *
 * @author SmallStrong
 */
@NoArgsConstructor
@Accessors(chain = true)
@Data
@ApiModel(value = "Result")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result implements Serializable {

    private static final long serialVersionUID = 3337439376898084639L;

    /**
     * 常规信息
     */
    /**
     * 返回码
     */
    @ApiModelProperty(value = "返回码", example = "200")
    private Integer code;
    /**
     * 返回信息
     */
    @ApiModelProperty(value = "返回信息", example = "成功")
    private String msg;
    /**
     * 返回数据
     */
    @ApiModelProperty(value = "返回数据")
    private Object data;

    /**
     * 额外信息
     */
    /**
     * 服务id
     */
    @ApiModelProperty(value = "服务id", example = "1")
    private String serverID;
    /**
     * 数据总条数
     */
    @ApiModelProperty(value = "数据总条数", example = "100")
    private Integer total;
    /**
     * 每页条数
     */
    @ApiModelProperty(value = "每页条数", example = "20")
    private Object rows;


    public static Result complete(Integer code, String msg, Object data) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    /**
     * 进行增删改时使用
     */
    public static Result success() {
        return Result.success(null, "操作成功！");
    }

    /**
     * 成功，传入data（使用最多）
     *
     * @param data
     * @return
     */
    public static Result success(Object data) {
        return Result.success(data, "请求成功！");
    }

    /**
     * 成功，传入data 和 msg
     *
     * @param data
     * @param msg
     * @return
     */
    public static Result success(Object data, String msg) {
        return Result.complete(ResultEnum.SUCCESS.getCode(), msg, data);
    }

    /**
     * 失败
     *
     * @return
     */
    public static Result error() {
        return Result.error("请求失败！");
    }

    /**
     * 失败 传入 msg
     *
     * @param msg
     * @return
     */
    public static Result error(String msg) {
        return Result.error(msg, ResultEnum.FAILURE);
    }

    public static Result error(String msg, ResultEnum resultEnum) {
        Result result = new Result();
        result.setCode(resultEnum.getCode());
        result.setMsg(msg);
        return result;
    }

    public static Result error(ResultEnum resultEnum) {
        return Result.complete(resultEnum.getCode(), resultEnum.getData(), null);
    }

    public static Result error(ResultEnum resultEnum, Object data) {
        return Result.complete(resultEnum.getCode(), resultEnum.getData(), data);
    }

    public static Result error(Object data) {
        return Result.complete(ResultEnum.UNKNOWNERROR.getCode(), ResultEnum.UNKNOWNERROR.getData(), data);
    }

    public static Result error(String msg, Object data) {
        return Result.complete(ResultEnum.UNKNOWNERROR.getCode(), msg, data);
    }

    public static Result TokenExp() {
        Result result = new Result();
        result.setCode(ResultEnum.EREOR.getCode());
        result.setMsg("凭证已过期，请重新登录");
        return result;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                ", serverID='" + serverID + '\'' +
                ", total=" + total +
                ", rows=" + rows +
                '}';
    }

    public String toJsonString() {
        return "{" +
                "\"code\":\"" + code +
                "\", \"msg\":\"" + msg +
                "\", \"data\":[" + (data == null ? "" : data) +
                "]}";
    }
}

